package com.android.mios.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.android.mios.db.MainDBAdapter;
import com.android.mios.util.Settings;
import android.database.Cursor;
import android.graphics.Bitmap;

public class Device {
	
	public int serialNumber;
	public int id;
	public String name;
	public int room;
	public int category;
	public int state;
	public String comment;
	public Bitmap lastPreview = null; // used to keep the last camera preview
	public Map<String, String> settings = new HashMap<String, String>();
	
	
     public static List<Device> findDevicesByCategory(Map<String, Device> devices, int category) {
		
		List<Device> results = new ArrayList<Device>();
		
		Collection<Device> devicesCollection = devices.values();
		for (Device device: devicesCollection) {
			
			if (device.category == category) {
				results.add(device);
			}
		}
		return results;
	}
	
	
	public static List<Device> findUnHideDevicesByCategory(Map<String, Device> devices, int category) {
		
		List<Device> results = new ArrayList<Device>();
		
		ArrayList<String> arrayOfUnSelectedDevice=new ArrayList<String>();
		Cursor cursor = MainDBAdapter.getInstance().getHideDevice(
				Settings.getInstance().getCurrentUsername());
		if (cursor != null) {
			while (cursor.moveToNext()) {
				String mID = cursor.getString(cursor.getColumnIndex("MID"));
				String mVeraSerial = cursor.getString(cursor
						.getColumnIndex("MVeraSerial"));
				arrayOfUnSelectedDevice.add(mVeraSerial + mID);
			}
		}

		Collection<Device> devicesCollection = devices.values();
		for (Device device: devicesCollection) {
			
			if (device.category == category && !arrayOfUnSelectedDevice.contains(device.serialNumber+""+device.id)) {
				results.add(device);
			}
		}
		return results;
	}
	
	public static Device findDeviceById(Map<String, Device> devices, int serialNumber , int id) {
		
		Collection<Device> devicesCollection = devices.values();
		for (Device device: devicesCollection) {
			if ( device.serialNumber==serialNumber && device.id == id) {
				return device;
			}
		}
		return null;
	}

//	public static List<Device> findLights(Map<String, Device> devices) {
//	
//		List<Device> dimmableLignts = findUnHideDevicesByCategory(devices, Category.DIMMABLE_LIGHT);
//		List<Device> lights = findUnHideDevicesByCategory(devices, Category.SWITCH);
//	    lights.addAll(dimmableLignts);
//		
//		return lights;
//	}
//	public static List<Device> findCameras(Map<String, Device> devices) {
//		
//		List<Device> securityCameras = findUnHideDevicesByCategory(devices, Category.CAMERA);
//		return securityCameras;
//	}
	
	public static List<Device> findDevicesByRoom(List<Device> devices,int serialNumber, int room) {
		
		List<Device> roomDevices = new ArrayList<Device>();
		for (Device device: devices) {
			if (device.serialNumber==serialNumber && device.room == room) {
				roomDevices.add(device);
			}
		}
		return roomDevices;
	}

	@Override
	public String toString() {
		if (name == null || name.trim().length() == 0) {
			return "No name";
		} else {
			return name;
		}
	}
	
	public static void traceCategory(Map<String, Device> allDevices, int category) {
		List<Device> devices = findUnHideDevicesByCategory(allDevices, category);
		for (Device device: devices) {
			String trace = "Name: " + device.name + ", category: " + device.category + ", state: " + device.state;
			Iterator<String> keys = device.settings.keySet().iterator();
			while (keys.hasNext()) {
				String key = keys.next();
				trace += (", " + key + ": " + device.settings.get(key));
			}
		
		}
	}
	
}
